package org.itboys.admin.service.lasted;

import org.itboys.admin.entity.lasted.SystemMenu;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class SystemMenuOperateService extends BaseMongoService<SystemMenu> {



	@Resource(name = "adminDS")
	private MongoDataSource ds;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<SystemMenu> getEntityClass() {
		return SystemMenu.class;
	}

	/**
	 * 获取系统菜单及附属功能
	 *
	 * @return
	 */
	public List<SystemMenu> getTree(){
		Map<String,Object> param =new HashMap<String, Object>();
		param.put("isDeleted",0);
		param.put("sort","sequence");
		param.put("order","ASC");
		/*if(ListUtil.isNotNull(menuIds)){
			param.put("id in",menuIds);
		}*/
		List<SystemMenu> all= super.list(param);
		List<SystemMenu> pMenu = new ArrayList<SystemMenu>();
		List<SystemMenu> cMenu = new ArrayList<SystemMenu>();
		List<SystemMenu> operate = new ArrayList<SystemMenu>();

		for(SystemMenu c:all){
		//	c.setIsMenu(1);
			if(c.getParentId().longValue()==0l){
				pMenu.add(c);
			}else{
				if(c.getIsMenu()==1){
					cMenu.add(c);
				}else{
					operate.add(c);
				}

			}
		//	super.updateExceptEmpty(c.getId(),c);
		}

		//组装TREE
		for(SystemMenu p:pMenu){
			List<SystemMenu> childMenu = new ArrayList<SystemMenu>();
			for(SystemMenu c:cMenu){

				if(c.getParentId().longValue()==p.getId()){
					//匹配菜单功能
					List<SystemMenu> childOperate = new ArrayList<SystemMenu>();
					 for(SystemMenu o:operate){
						 if(o.getParentId().longValue()==c.getId()) {
							 		childOperate.add(o);
							    }
						 }
					c.setChildren(childOperate);
					childMenu.add(c);
				}
			}
			p.setChildren(childMenu);
		}
		return pMenu;

		//return null;
	}

	/**
	 * 新增或修改菜单的功能
	 * @param operate
	 */
	public void saveMenu(SystemMenu operate){
		//查询父菜单的接口根目录
		SystemMenu parent =super.getById(operate.getParentId());
		operate.setRootUrl(parent.getRootUrl());
		if(operate.getId()==0l){
			//2017年3月16日
			operate.setIsMenu(0);
			super.save(operate);
		}else{
			super.updateExceptEmpty(operate.getId(),operate);
		}
		//return null;
	}

	/**
	 * 更改菜单的接口根目录
	 * @param id
	 * @param rootUrl
	 */
	public void editMenuRootUrl(Long id,String rootUrl){
		//找到菜单下的功能
		Map<String,Object> param =new HashMap<String,Object>();
		param.put("isDeleted",0);
		param.put("parentId",id);
		param.put("isMenu",0);
		List<SystemMenu> operates = super.list(param);
		for(SystemMenu operate:operates){
			operate.setRootUrl(rootUrl);
			super.update(operate.getId(),"rootUrl",rootUrl);
		}

		super.update(id,"rootUrl",rootUrl);

	}
	/**
	 * 单个查询
	 * @param id
	 * @return
	 */
	public  SystemMenu findOne(Long id,Long parentId){
		//SystemMenu operate = null;
		if(LongUtil.isNotZreo(id)){
			return super.getById(id);
		}else{
			SystemMenu menu = super.getById(parentId);
			SystemMenu operate = new SystemMenu();
			operate.setId(0);
			operate.setParentId(parentId);
			operate.setRootUrl(menu.getRootUrl());
			return operate;
		}


	}
}
